package test;

import java.util.List;

import org.hibernate.HibernateException;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import trip.*;
import dao.*;

public class CompanyDAOTest extends Assert {
	
	private CompanyDAO cD = Factory.getInstance().getCompanyDAO();
	private RouteDAO rD = Factory.getInstance().getRouteDAO();
	private TripDAO tD = Factory.getInstance().getTripDAO();
	
	@DataProvider
	public Object[][] companyData() {
		return new Object[][] {
				{"test.1", true},
				{null, false},
				{"nameLengthGreaterThenMaxGREATERGREATERGREATER", false},
		};
	}
	
	@Test(dataProvider="companyData")
	public void dataTest(String name, boolean expected)	{
		try {
			Integer count = cD.getAll().size();
			assertNotNull(count);
			Company company = new Company(1, name);
			cD.create(company);
			Company tmpComp = cD.getById(company.getId());
			assertNotNull(tmpComp);
			assertEquals(tmpComp.getId(), company.getId());
			assertEquals(tmpComp.getName(), company.getName());
			company.setName(company.getName() + "new");
			cD.update(company);
			tmpComp = cD.getById(company.getId());
			assertNotNull(tmpComp);
			assertEquals(tmpComp.getId(), company.getId());
			assertEquals(tmpComp.getName(), company.getName());
			cD.delete(tmpComp);
			tmpComp = cD.getById(company.getId());
			assertNull(tmpComp);
			assertEquals(cD.getAll().size(), count.intValue());
			assertTrue(expected);
		} catch (HibernateException ex) {
			assertFalse(expected);
		}
	}
	
	@DataProvider
	public Object[][] companyName() {
		return new Object[][] {
				{"templ", true},
				{"", true},
				{"TEMPL", true},
				{null, false},
				{"LengthGreaterThenMaxGREATERGREATERGREATER", false}
		};
	}
	
	@Test(dataProvider="companyName")
	public void getByNameTest(String name, boolean expected) {
		List<Company> comps = cD.getAll();
		assertNotNull(comps);
		Company company = new Company();
		company.setName("template");
		cD.create(company);
		List<Company> cByN = cD.getByName(name);
		assertNotNull(cByN);
		if (cByN.size() == 0) {
			assertFalse(expected);
			for(Company comp : comps) {
				if (name != null)
					assertFalse(comp.getName().toLowerCase().indexOf(name.toLowerCase()) >= 0);
			}
		}
		else {
			assertNotNull(name);
			for(Company comp : cByN)
				assertTrue(comp.getName().toLowerCase().indexOf(name.toLowerCase()) >= 0);
		}
		cD.delete(company);
	}
	
	@Test
	public void getByRouteTest() {
		List<Company> comps = cD.getAll();
		assertNotNull(comps);
		List<Route> routes = rD.getAll();
		assertNotNull(routes);
		List<Trip> trips = tD.getAll();
		assertNotNull(trips);
		for(Route route : routes) {
			List<Company> cByR = cD.getByRoute(route);
			assertNotNull(cByR);
			if (cByR.size() != 0) {
				for(Company company : cByR) {
					boolean found = false;
					for(Trip trip : trips) {
						if (trip.getCompany().equals(company) && trip.getRoute().equals(route)) {
							found = true;
							break;
						}
					}
					assertTrue(found);
				}
			} 
			else {
				boolean found = false;
				for(Trip trip : trips) {
					if (trip.getRoute().equals(route)) {
						found = true;
						break;
					}
				}
				assertFalse(found);
			}
		}
	}
}
